package com.example.onlineshop.db;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.widget.Toast;

import com.example.onlineshop.Domain.CartEntity;

import java.util.ArrayList;
import java.util.List;

/**
 * 购物车数据库操作类
 */
public class SqliteCarOpenHelper extends SQLiteOpenHelper {

    private static final String CREATE_TASK = "create table cart ("
            + "id integer primary key autoincrement, "
            + "title text," +
            "imgPath text," +
            "price text," +
            "ratePrice text," +
            "num integer)";
    private Context mContext;

    public SqliteCarOpenHelper(Context context,
                               String name,
                               SQLiteDatabase.CursorFactory factory,
                               int version) {
        super(context, name, factory, version);
        mContext = context;
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(CREATE_TASK);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("drop table if exists Book");
        onCreate(db);
    }

    // 增加数据
    public long insertData(String title, String imgPath, String price, int num, String ratePrice) {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put("title", title);
        values.put("imgPath", imgPath);
        values.put("price", price);
        values.put("ratePrice", ratePrice);
        values.put("num", num);
        long result = db.insert("cart", null, values);
//        if (result == -1) {
//            Toast.makeText(mContext, "数据添加失败", Toast.LENGTH_SHORT).show();
//        } else {
//            Toast.makeText(mContext, "数据添加成功", Toast.LENGTH_SHORT).show();
//        }
        db.close();
        return  result;
    }

    // 删除数据
    public void deleteData(String title) {
        SQLiteDatabase db = this.getWritableDatabase();
        int result = db.delete("cart", "title = ?", new String[] { title });
        if (result == 0) {
            Toast.makeText(mContext, "数据删除失败", Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(mContext, "数据删除成功", Toast.LENGTH_SHORT).show();
        }
        db.close();
    }
    // 删除全部数据
    public void deleteAll() {
        SQLiteDatabase db = this.getWritableDatabase();
        int result = db.delete("cart", null, null );
        if (result == 0) {
            Toast.makeText(mContext, "数据删除失败", Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(mContext, "数据删除成功", Toast.LENGTH_SHORT).show();
        }
        db.close();
    }

    // 修改数据
    public void updateData(String title, String imgPath, String price, int num, String ratePrice) {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put("title", title);
        values.put("imgPath", imgPath);
        values.put("price", price);
        values.put("ratePrice", ratePrice);
        values.put("num", num);
        int result = db.update("cart", values, "title = ?", new String[] { title});
        if (result == 0) {
            Toast.makeText(mContext, "数据修改失败", Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(mContext, "数据修改成功", Toast.LENGTH_SHORT).show();
        }
        db.close();
    }
    /**
     * 查询数据库中所有的购物车数据
     *
     * @return 返回包含所有购物车项目的列表，每个项目都是一个CartEntity实例
     */
    public List<CartEntity> QueryAll(){
        // 获取可读数据库实例
        SQLiteDatabase db = this.getReadableDatabase();
        List<CartEntity> list=new ArrayList<>();

        // 执行查询操作
        Cursor cursor = db.query("cart", null, null, null, null, null, null, null);

        // 遍历查询结果，并将每条数据封装为CartEntity对象，添加到列表中
        while(cursor.moveToNext()){
            int id = cursor.getInt(0);
            String title = cursor.getString(1);
            String imgPath = cursor.getString(2);
            String price = cursor.getString(3);
            String ratePrice = cursor.getString(4);
            int num = cursor.getInt(5);

            CartEntity car=new  CartEntity();
            car.setId(id);
            car.setTitle(title);
            car.setNum(num);
            car.setImgPath(imgPath);
            car.setPrice(price);
            car.setRatePrice(ratePrice);

            list.add(car);
        }

        return  list;
    }
    /**
     * 查询指定商品名称在购物车数据库中的信息是否存在，并返回对应的商品实体列表。
     *
     * @param name 要查询的商品名称。
     * @return 返回一个包含查询到的商品实体的列表。如果未查询到任何商品，则返回空列表。
     */
    public List<CartEntity> QueryByName(String name){
        // 获取可读数据库实例
        SQLiteDatabase db = this.getReadableDatabase();
        List<CartEntity> list=new ArrayList<>();

        // 执行查询操作，获取符合条件的所有购物车商品记录
        Cursor cursor = db.query("cart",null, "title = ?",new String[]{name}, null, null, null, null);

        // 遍历查询结果，并将每条记录转换为CartEntity对象，添加到列表中
        while(cursor.moveToNext()){
            int id = cursor.getInt(0);
            String title = cursor.getString(1);
            String imgPath = cursor.getString(2);
            String price = cursor.getString(3);
            String ratePrice = cursor.getString(4);
            int num = cursor.getInt(5);

            CartEntity car=new  CartEntity();
            car.setId(id);
            car.setTitle(title);
            car.setNum(num);
            car.setImgPath(imgPath);
            car.setPrice(price);
            car.setRatePrice(ratePrice);
            list.add(car);
        }
        return  list;
    }


}

